package com.yunlankeji.stemcells.adapter;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.yunlankeji.ganxibaozhijia.R;
import com.yunlankeji.ganxibaozhijia.databinding.AddImgBinding;
import com.yunlankeji.stemcells.BaseApplication;

import java.util.List;

/**
 * @author yang
 */
public class ImgAddAdapter extends RecyclerView.Adapter<ImgAddAdapter.ViewHolder> {

    private List<String> uris;
    private AddImgBinding binding;
    private AddImgOnClickListener addImgOnClickListener;

    public ImgAddAdapter(List<String> uris, AddImgOnClickListener addImgOnClickListener) {
        this.uris = uris;
        this.addImgOnClickListener = addImgOnClickListener;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        binding = AddImgBinding.inflate(LayoutInflater.from(BaseApplication.getAppContext()), parent, false);
        return new ViewHolder(binding.getRoot());
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Log.d("TAG", "onBindViewHolder: "+position);
        if(position==uris.size()){
            holder.vDelete.setVisibility(View.GONE);
            holder.imgDelete.setVisibility(View.GONE);
            holder.img.setOnClickListener( v -> addImgOnClickListener.addImg());
            Glide.with(BaseApplication.getAppContext())
                    .load(R.mipmap.sctp)
                    .apply(RequestOptions.bitmapTransform(new RoundedCorners(20)))
                    .into(holder.img);
        }else {
            holder.vDelete.setVisibility(View.VISIBLE);
            holder.imgDelete.setVisibility(View.VISIBLE);
            holder.img.setOnClickListener(v -> {});
            holder.vDelete.setOnClickListener(v -> {
                Log.d("TAG", "onBindViewHolder1111111111: "+position);
                uris.remove(position);
                notifyDataSetChanged();
            });
            Glide.with(BaseApplication.getAppContext())
                    .load(uris.get(position))
                    .apply(RequestOptions.bitmapTransform(new RoundedCorners(20)))
                    .into(holder.img);
        }
    }

    @Override
    public int getItemCount() {
        return uris==null? 1 : uris.size()+1;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        final ImageView img;
        final View vDelete;
        final ImageView imgDelete;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            img = itemView.findViewById(R.id.img);
            vDelete = itemView.findViewById(R.id.v_delete);
            imgDelete = itemView.findViewById(R.id.img_delete);
        }
    }

    /**
     * 添加图片回调接口
     */
    public interface AddImgOnClickListener{
        /**
         * 添加图片
         */
       void addImg();
    }
}
